package com.company.cloud.tag.modules.mgr.controller;

import java.util.function.Function;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.tag.modules.app.controller.req.LabelQuery4PageParam;
import com.company.cloud.tag.modules.base.entity.LabelInfo;
import com.company.cloud.tag.modules.base.service.LabelInfoService;
import com.company.cloud.tag.modules.cache.models.LabelModel;
import com.company.cloud.tag.modules.cache.utils.TagCacheKey;
import com.company.cloud.tag.modules.cache.utils.TagConvertUtils;
import com.company.cloud.tag.modules.mgr.controller.req.LabelSaveParam;
import com.company.cloud.tag.modules.mgr.controller.req.LabelUpdateParam;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Authc
@RestController
@RequestMapping("/mgr/label/tag")
@Api(tags="Mgr_Label_标签管理")
public class LabelMgrController {

    @Autowired
    private LabelInfoService labelInfoService;

    @PostMapping("/query")
    @ApiOperation(value="查询",response = LabelModel.class)
    public BaseResult query(@RequestBody LabelQuery4PageParam param) {
        IPage<LabelInfo> page = labelInfoService.page(new Page<LabelInfo>(param.getPage(),param.getRows()),
                new QueryWrapper<LabelInfo>()
                        .eq(StringUtils.hasText(param.getLabelId()),"label_id",param.getLabelId())
                        .like(StringUtils.hasText(param.getLabelName()),"label_name",param.getLabelName())
                        .eq(StringUtils.hasText(param.getLabelTypeNo()),"label_type_no",param.getLabelTypeNo())
                        .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                        .orderByDesc("orders")
        );

        IPage<LabelModel> data = page.convert(new Function<LabelInfo, LabelModel>() {
            @Override
            public LabelModel apply(LabelInfo entity) {
                return TagConvertUtils.getLabel(entity);
            }
        });
        return BaseResult.ok(data);
    }

    @GetMapping("/get")
    @ApiOperation(value="查询")
    public BaseResult get(@RequestParam("labelId") String labelId) {
        LabelInfo entity = labelInfoService.getById(labelId);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        return BaseResult.ok(TagConvertUtils.getLabel(entity));
    }

    @PostMapping("/save")
    @StaffLogc(title = "Mgr_Label_标签管理新增", action = "Mgr_Label_标签管理新增", actionAdo = "Mgr_Label_标签管理")
    @ApiOperation(value="新增")
    public BaseResult save(@RequestBody LabelSaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        LabelInfo entity = new LabelInfo();
        BeanUtil.copyProperties(params, entity);
        entity.setLabelId(TagCacheKey.getLabelId());
        entity.setRowsState(BaseConstant.EnabledEnum.ENABLED.getCode());
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        labelInfoService.save(entity);
        return BaseResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value="修改")
    @StaffLogc(title = "Mgr_Label_标签管理修改", action = "Mgr_Label_标签管理修改", actionAdo = "Mgr_Label_标签管理")
    public BaseResult update(@RequestBody LabelUpdateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        LabelInfo entity = labelInfoService.getById(params.getLabelId());
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        BeanUtil.copyProperties(params, entity);
        entity.addEditParam(userInfo);
        labelInfoService.updateById(entity);
        return BaseResult.ok();
    }

    @GetMapping("/delete")
    @ApiOperation(value="删除")
    @StaffLogc(title = "Mgr_Label_标签管理删除", action = "Mgr_Label_标签管理删除", actionAdo = "Mgr_Label_标签管理")
    public BaseResult delete(@RequestParam("labelId")String labelId) {
        LabelInfo entity = labelInfoService.getById(labelId);
        if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
        entity.addEditParam(userInfo);
        labelInfoService.updateById(entity);
        return BaseResult.ok();
    }
}
